<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改商户</title>
    <style>
        .wu-pos-a {
            box-sizing: border-box;
            padding-top: 39px;
            width: 20%;
            left: auto;
            right: 0;
        }
        .wu-m5>input{
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
<div class="pos-r">
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span>
        商户管理
        <span class="c-gray en">&gt;</span>
        商户信息
        <span class="c-gray en">&gt;</span>
        修改商户
    </nav>
    <!--<div class="pos-a wu-pos-a">-->
        <!--<div class="wu-tree">商户可服务类型</div>-->
        <!--<div class="wu-treeBox">-->
            <!--<ul id="edit-merchantAddTree" class="ztree"></ul>-->
        <!--</div>-->

        <!--<div id="contentWu"></div>-->
    <!--</div>-->

    <!--<div style="margin-right:22%">-->
    <div>
        <div class="page-container">
            <form action="" method="get" class="form form-horizontal" id="edit-form-mer">
                <div class="form-unit">商户信息</div>
                <input style="display: none;" id="edit-userNames-temp"/>
                <input style="display: none;" id="edit-bizId" name="bizId"/>
                <input style="display: none;" id="edit-merchantArray" name="array"/>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>商户名称：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" value="" placeholder="必填项" id="edit-bizNames" name="bizName">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>商户状态：</label>
                    <div class="formControls col-xs-4 col-sm-4 skin-minimal">
                        <div class="radio-box">
                            <input type="radio" id="edit-state-1" name="bizState" value="0" checked>
                            <label for="edit-state-1">开启</label>&nbsp;&nbsp;&nbsp;&nbsp;
                        </div>
                        <div class="radio-box">
                            <input type="radio" id="edit-state-2" name="bizState" value="1">
                            <label for="edit-state-2">禁用</label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>联系电话：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" value="" placeholder="必填项" id="edit-phones" name="phone">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>商户账号：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" placeholder="必填项" name="userNames" id="edit-userNames" value="">
                    </div>
                    <span id="edit-usernameSpan"></span>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>商户密码：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="password" class="form-control" placeholder="不修改无需填写" name="password" id="edit-passwords">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>角色：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" placeholder="" name="roleId" id="edit-roleIds">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>联系地址：</label>
                    <div class="formControls col-xs-4 col-sm-4">
                        <input type="text" class="form-control" placeholder="" name="address" id="edit-addresss">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                        <input onclick="platformMerchantMerchantEditFun.submitAction();return false;" class="btn btn-primary radius" value="&nbsp;&nbsp;提交&nbsp;&nbsp;"/>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    //商户管理，添加服务
    var platformMerchantMerchantEditFun;

    $(function () {
        //加载角色
        $.ajax({
            type: "GET",
            url: "menu/getBizRoles",
            dataType: "json",
            success: function (data) {
                $("#edit-roleIds").select2({
                    data: data,
                    placeholder: '请选择',
                    allowClear: true
                })
                $('#edit-roleIds').on('select2:select', function (e) {
                    $(e.target).siblings('I').removeClass('glyphicon glyphicon-remove').addClass('glyphicon glyphicon-ok').show().siblings('SMALL').hide().parents('.form-group').removeClass('has-error').addClass('has-success')
                });
                $('#edit-roleIds').on('select2:unselect', function (e) {
                    $(e.target).siblings('I').removeClass('glyphicon glyphicon-ok').addClass('glyphicon glyphicon-remove').show().siblings('SMALL').show().parents('.form-group').removeClass('has-success').addClass('has-error')
                });
            },
            error: function () {
                alert("error");
            }
        });
    });

        platformMerchantMerchantEditFun = {
        init: function () {
            let _this = this;
            var bizId = sessionStorage.getItem("merchant-bizId");
            _this.modify_array(bizId);
            _this.bootstrapvalidatorForm();

        },
        //修改，查询单条
        modify_array: function (index) {
            var _this = this;

            $.ajax({
                type: "get",
                url: "biz/getBizTenant",
                async: false,
                data: {bizId: index},
                dataType: "json",
                success: function (result) {
                    if (result.state) {
                        var data = result.data;
                        $("#edit-bizNames").val(data.bizName);
                        $("#edit-phones").val(data.phone);
                        $("#edit-addresss").val(data.address);
                        $("#edit-userNames").val(data.userName);
                        $("#edit-userNames-temp").val(data.userName);
                        $("#edit-bizId").val(data.bizId);
                        var state = data.bizState;
                        $("#edit-state-" + (state + 1)).prop("checked", true);
                        var ddds = $("#edit-roleIds").select2();    //获取selectid
                        ddds.val(data.roleId).trigger("change");    //设置 value 为four的 option 为选中状态

                        $("#editMerImg").fileinput("clear");
                        var resList = data.imgList;
                        if (resList != null) {
                            var len = resList.length;
                            var width = "85px";
                            if (len == 1) {
                                width = "160px";
                            }
                            var img = new Array();
                            for (var i = 0; i < len; i++) {
                                img.push("<img src='files" + resList[i]["url"] + "' eid='" + resList[i]["urlId"] + "' class='file-preview-image' style='width:" + width + ";height:" + width + ";' onclick='window.open(this.src)'>");
                            }
                            $("#editMerImg").fileinput("refresh", {initialPreview: ""});//必加多个资源有重复图片
                            $("#editMerImg").fileinput("refresh", {initialPreview: img});
                            $("#edit-form-mer").find(".file-thumbnail-footer").remove();
                            $("#edit-form-mer").find(".file-drop-zone-title").remove();
                            $("#edit-form-mer").find(".file-preview-frame").css("height", "100px");
                        }
                    }
                    else {
                        alert(result.message);
                    }
                },
                error: function () {
                    alert("error");
                }
            });
            _this.loadTreeChecked(index);
        },
        //验证用户名是否存在
        usernameCheck: function (par) {
            if($("#edit-userNames-temp").val() == par){
                $("#edit-usernameSpan").text("该商户账号可使用！");
                $("#edit-usernameSpan").css("color", "green");
                return;
            }
            $.ajax({
                type: "GET",
                url: "biz/checkTenantName",
                data: {userName: par},
                dataType: "json",
                success: function (data) {
                    $("#edit-usernameSpan").text(data.message);
                    if (data.state) {
                        $("#edit-usernameSpan").css("color", "green");
                    } else {
                        $("#edit-usernameSpan").css("color", "red");
                    }
                },
                error: function () {
                    alert("error");
                }
            });
        },
        //表单提交
        submitAction: function () {
            //bootstrap验证
//            let bootstrapValidator = $("#edit-form-mer").data('bootstrapValidator');
//            bootstrapValidator.validate();
//            if(bootstrapValidator.isValid())
//                $("#edit-form-mer").submit();
//            else return;

//            var zTreeTemp = $.fn.zTree.getZTreeObj("edit-merchantAddTree");
//            var nodesTemp = zTreeTemp.getCheckedNodes(true);//在提交表单之前将选中的checkbox收集
//            if(nodesTemp.length == 0){
//                layer.alert("请选择至少一个服务项目！", {icon: 0});
//                return false;
//            }

//            //负责人
//            var sysUserId = $("#edit-sysUserIds").select2('val');  //获取selectid
//            if (sysUserId == null || sysUserId == undefined || sysUserId == "") {
//                layer.alert("负责人不能为空！", {icon: 0});
//                return false;
//            }

//            var treeObj = $.fn.zTree.getZTreeObj("edit-merchantAddTree");
//            var nodes = treeObj.getCheckedNodes(true);//在提交表单之前将选中的checkbox收集
//            var array = new Array();
//            for (var i = 0; i < nodes.length; i++) {
//                if($("#wuEdit"+nodes[i].id).val() == ""){
//                    array.push(0);
//                    continue;
//                }
//                array.push(nodes[i].id);
//                array.push($("#wuEdit"+nodes[i].id).val()*100);
//            }
//            $("#edit-merchantArray").val(array);

            //提交
            $.ajax({
                type: "GET",
                url: "biz/modifyTenant",
                data: $("#edit-form-mer").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.state) {
                        layer.msg(data.message, {icon: 1});
                        //关闭窗口
                        removeIframe();
                        platformMerchantMerchantFun.searchAction();
                    } else {
                        alert(data.message);
                    }
                },
                error: function () {
                    alert("error");
                }
            });
        },
//        //加载商户可服务类型
//        serviceType: function () {
//            $.ajax({
//                type: "get",
//                async: false,
//                url: "biz/configureServiceType",
//                dataType: "json",
//                success: function (data) {
//                    var merchantAddSetting = {
//                        check: {
//                            enable: true
//                        },
//                        data: {
//                            simpleData: {
//                                enable: true
//                            }
//                        },
//                        callback : {
//                            onCheck : function (event, treeId, treeNode) {
//                                console.log(treeNode.name,treeNode.id,treeNode.checked);
//                                switch(treeNode.id)
//                                {
//                                    case '1':
//                                        treeNode.checked && ($("#wuEdit1").attr("disabled",false).focus()) || (!treeNode.checked) && ($("#wuEdit1").attr("disabled",true).val(""));
//                                        break;
//                                    case '2':
//                                        treeNode.checked && ($("#wuEdit2").attr("disabled",false).focus())|| (!treeNode.checked) && ($("#wuEdit2").attr("disabled",true).val(""));
//                                        break;
//                                    case '3' :
//                                        treeNode.checked && ($("#wuEdit3").attr("disabled",false).focus())|| (!treeNode.checked) && ($("#wuEdit3").attr("disabled",true).val(""));
//                                        break;
//                                    case '4' :
//                                        treeNode.checked && ($("#wuEdit4").attr("disabled",false).focus())|| (!treeNode.checked) && ($("#wuEdit4").attr("disabled",true).val(""));
//                                        break;
//                                    default:
//                                        break;
//                                }
//                            }
//                        }
//                    };
//                    $.fn.zTree.init($("#edit-merchantAddTree"), merchantAddSetting, data);
//                    var treeObj = $.fn.zTree.getZTreeObj("edit-merchantAddTree");
//                    treeObj.expandAll(true);
//                    //父节点不能选择
//                    var nodes = treeObj.transformToArray(treeObj.getNodes());
//                    for (var i = 0, l = nodes.length; i < l; i++) {
//                        if (nodes[i].isParent) {
//                            treeObj.setChkDisabled(nodes[i], true);
//                        }
//                    }
//                },
//                error: function () {
//                    alert("error");
//                }
//            });
//        },
//        //加载右侧树选中节点
//        loadTreeChecked: function (index) {
//            $.ajax({
//                type: "GET",
//                url: "biz/getBizServiceType?bizId=" + index,
//                dataType: "json",
//                success: function (data) {
//                    //首先取消所有选中状态
//                    var treeObj = $.fn.zTree.getZTreeObj("edit-merchantAddTree");
//                    treeObj.checkAllNodes(false);
//                    treeObj.cancelSelectedNode();
//
//                    //获取所有节点的信息data
//                    for (var i = 0; i < data.length; i++) {
//                        var zTree = $.fn.zTree.getZTreeObj("edit-merchantAddTree");
//                        var node = zTree.getNodeByParam("id", data[i].serviceType);
//                        node.checked = true;
//                        //设置选中之后，需要更新一次，否则无法选中该节点
//                        zTree.updateNode(node);
//                    }
//
//                    //给提成input设置值
//                    for (var i = 0; i < data.length; i++) {
//                        $("#wuEdit"+data[i].serviceType).attr("disabled",false);
//                        $("#wuEdit"+data[i].serviceType).val(data[i].deduct/100);
//                    }
//
//                },
//                error: function () {
//                    alert("error");
//                }
//            });
//        },
        //bootstrap验证
        bootstrapvalidatorForm: function () {
            $('#edit-form-mer').bootstrapValidator({
                message: 'This value is not valid',
                excluded : [':disabled'],
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    bizName: {
                        validators: {
                            notEmpty: {
                                message: '商户名称不能为空'
                            },
                            stringLength: {
                                min: 2,
                                max: 100,
                                message: '商户名称长度必须在2到100位之间'
                            }
                        }
                    },
                    phone: {
                        validators: {
                            notEmpty: {
                                message: '联系电话不能为空'
                            },
                            regexp: {
                                regexp: /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/,
                                message: '联系电话格式不正确'
                            }
                        }
                    },
                    userNames: {
                        validators: {
                            notEmpty: {
                                message: '商户账号不能为空'
                            },
                            stringLength: {
                                min: 4,
                                max: 50,
                                message: '商户账号长度必须在4到50位之间'
                            },
                            threshold:4,
                            remote:{
                                url:"biz/checkTenantName",
                                data:{userNameHide:$('#edit-userNames-temp').val()},
                                message: "该商户账号已存在",
                                delay: 2000,
                                type:"GET"
                            }
                        }
                    },
                    password: {
                        validators: {
                            stringLength: {
                                min: 6,
                                max: 32,
                                message: '商户密码长度必须在6到32位之间'
                            }
                        }
                    },
                    roleId: {
                        validators: {
                            notEmpty: {
                                message: '角色不能为空'
                            }
                        }
                    },
                    wu1: {
                        validators: {
                            notEmpty: {
                            message: '洗车提成不能为空'
                            },
                            regexp: {
                                regexp: /^([1-9]\d?(\.\d{1,2})?|0\.\d{1,2}|100)$/,
                                message: '洗车提成格式不正确'
                            }
                        }
                    },
                    wu2: {
                        validators: {
                            notEmpty: {
                                message: '美容提成不能为空'
                            },
                            regexp: {
                                regexp: /^([1-9]\d?(\.\d{1,2})?|0\.\d{1,2}|100)$/,
                                message: '美容提成格式不正确'
                            }
                        }
                    },
                    wu3: {
                        validators: {
                            notEmpty: {
                                message: '保养提成不能为空'
                            },
                            regexp: {
                                regexp: /^([1-9]\d?(\.\d{1,2})?|0\.\d{1,2}|100)$/,
                                message: '保养提成格式不正确'
                            }
                        }
                    },
                    wu4: {
                        validators: {
                            notEmpty: {
                                message: '维修提成不能为空'
                            },
                            regexp: {
                                regexp: /^([1-9]\d?(\.\d{1,2})?|0\.\d{1,2}|100)$/,
                                message: '维修提成格式不正确'
                            }
                        }
                    }
                }
            });
        }
    };

    platformMerchantMerchantEditFun.init();

    $("#editMerImg").fileinput({
        language: "zh", //设置语言
        uploadUrl: "file/upload", //上传的地址
        allowedFileExtensions: ["jpg", "png", "gif"],//接收的文件后缀
        maxFileCount: 5,
        showUpload: false,//是否显示上传按钮
        showRemove: false,//是否显示删除按钮
        showCaption: true,//是否显示输入框
        browseClass: "btn btn-primary", //按钮样式
        enctype: "multipart/form-data",
        showPreview: true,
        previewFileIcon: "<i class='fa fa-file'></i>",
        maxSize: 1024,//单位kb
        uploadAsync: false,//多图片一次上传
        allowedPreviewTypes: ["image"],
        uploadExtraData: function () {
            return extData;
        }
    }).on("fileuploaded", function (event, data) {
        // if (data.response.success == 0) {
        //     base.success("修改成功！");
        //     base.closeTab("修改商品");
        // }
        // else {
        //     base.error("修改成功，图片上传失败");
        // }
    }).on("change", function (event) {
        isChange = true;
    }).on("fileloaded", function () {
        // $("#edit-form-mer").find(".file-thumbnail-footer").remove();
        // $("#edit-form-mer").find(".file-drop-zone-title").remove();
    });

</script>
</body>
</html>